from tinkoff_bot.core.models.dto import UserDTO
from tinkoff_bot.infrastructure.database.models import User
from tinkoff_bot.infrastructure.database.uow import SQLAlchemyUoW


class UserService:
    def __init__(self, uow: SQLAlchemyUoW):
        self._uow = uow

    async def get_user_by_id(self, user_id: int) -> UserDTO:
        """Получаем пользователя по id"""

        return await self._uow.user_reader.get_user_by_id(user_id)

    async def get_user_by_telegram_id(self, user_tg_id: int) -> UserDTO:
        """Получаем пользователя по telegram id"""

        return await self._uow.user_reader.get_user_by_tg_id(user_tg_id)

    async def create_user(self, telegram_id: int, phone_number: str, username: str) -> None:
        """Создаем пользователя"""

        user = User(telegram_id=telegram_id, phone_number=phone_number, username=username)
        try:
            await self._uow.user_repo.create_user(user)
        except Exception as err:
            await self._uow.rollback()
            raise err

        await self._uow.commit()
